package com.bjtu.laorenyouhao.yigong.service.impl;

import com.bjtu.laorenyouhao.yigong.entity.YigongInfoEntity;
import com.bjtu.laorenyouhao.yigong.feign.LaorenFeignService;
import com.bjtu.laorenyouhao.yigong.service.YigongInfoService;
import com.bjtu.laorenyouhao.yigong.utils.RenyuanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjtu.common.utils.PageUtils;
import com.bjtu.common.utils.Query;

import com.bjtu.laorenyouhao.yigong.dao.LaorenYigongDao;
import com.bjtu.laorenyouhao.yigong.entity.LaorenYigongEntity;
import com.bjtu.laorenyouhao.yigong.service.LaorenYigongService;
import org.springframework.util.StringUtils;


@Service("laorenYigongService")
public class LaorenYigongServiceImpl extends ServiceImpl<LaorenYigongDao, LaorenYigongEntity> implements LaorenYigongService {

    @Autowired
    private LaorenFeignService laorenFeignService;

    @Autowired
    private YigongInfoService yigongInfoService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        String key = (String) params.get("key");
        QueryWrapper<LaorenYigongEntity> queryWrapper = new QueryWrapper<LaorenYigongEntity>();
        if (StringUtils.hasLength(key)){
            queryWrapper.eq("id",key)
                    .or().like("begin_date",key)
                    .or().eq("laoren_id", RenyuanUtils.getLaorenId(key,laorenFeignService))
                    .or().eq("yigong_id",
                    yigongInfoService.getOne(new QueryWrapper<YigongInfoEntity>().eq("name",key)).getId());
        }

        IPage<LaorenYigongEntity> page = this.page(
                new Query<LaorenYigongEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

}